﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Com.Framework;
using Com.Web.coffinegurunaru.AppLogic.Page;
using Com.Library.DB.Board;
using System.Web.Script.Serialization;
using Com.Framework.File;

namespace Com.Web.coffinegurunaru.aboutus
{
	public partial class contactus_write : WebPage
	{
		public int InputType { get; set; }
		protected void Page_Load(object sender, EventArgs e)
		{
			InputType = Request.QueryString["type"].Parse(0);
			this.LeftMenuType = 5;
		}

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);
			if (IsPostBack)
			{
				string com_formmail_check_safe = Request.Form["com_formmail_check_safe"].Parse(string.Empty);

				if (!string.IsNullOrEmpty(com_formmail_check_safe))
				{
					string category = Request.Form["category"].Parse(string.Empty).Trim();
					string name = Request.Form["name"].Parse(string.Empty).Trim();
					string phone = Request.Form["phone_no1"].Parse(string.Empty).Trim() + "-" +
						Request.Form["phone_no2"].Parse(string.Empty).Trim() + "-" +
						Request.Form["phone_no3"].Parse(string.Empty).Trim();
					string email = Request.Form["email1"].Parse(string.Empty).Trim() + "@" +
						Request.Form["email3"].Parse(string.Empty).Trim();
					string postNo = Request.Form["address_post1"].Parse(string.Empty).Trim() + "-" +
						Request.Form["address_post2"].Parse(string.Empty).Trim();
					string address = Request.Form["address"].Parse(string.Empty).Trim();
					string title = Request.Form["title"].Parse(string.Empty).Trim();
					string description = Request.Form["description"].Parse(string.Empty).Trim();

					string fileUrl = string.Empty;
					if (Request.Files.Count == 1 && Request.Files[0].ContentLength > 0)
						fileUrl = FileManager.UploadFor(Request.Files[0], FileType.Contact);

					Dictionary<string, object> addtionalData = new Dictionary<string, object>()
					{
						{"업무담당부서", GetDepart(category)},
						{"이름", name},
						{"연락처", phone},
						{"이메일", email},
						{"현주소", string.Format("({0}) {1}", postNo, address)}
					};

					string jsonData = (new JavaScriptSerializer()).Serialize(addtionalData);

					ArticleModel.Create((byte)ArticleModel.BoardSNEnum.Contact, Convert.ToInt32(category), title, description, jsonData, 0, WebCookies.UserID, fileUrl);

					ClientScript.RegisterStartupScript(this.GetType(), "success", "alert('문의사항이 보내졌습니다');location.href='/aboutus/contactus.aspx';", true);
				}
			}
		}

		private string GetDepart(string categoryNo)
		{
			switch(categoryNo)
			{
				case "0" :
					return "마케팅,기획,프로모션 제휴";
				case "1" :
					return "원,부자재 문의";
				case "2" :
					return "채용문의";
				case "3" :
					return "가맹점문의";
				case "4" : 
					return "고객의 소리";
				default :
					return "알수없음. categoryNo=" + categoryNo;
			}
		}
	}
}
